package com.plian.system.controller.ms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.entity.ms.QuarterInspect;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.ms.IQuarterInspectService;
import com.plian.system.vo.ms.QuarterInspectVO;
import com.plian.system.wrapper.ms.QuarterInspectWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.util.List;
import java.util.Optional;

/**
 * @author huzb
 * @create 2020-11-09 14:51
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/quarterinspect")
public class QuarterInspectController {

    private IQuarterInspectService iQuarterInspectService;

    private QuarterInspectWrapper quarterInspectWrapper;

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("季度监督检查报告-提交")
    public ObjResult submit(@Valid @RequestBody QuarterInspect quarterInspect) {
        if (Optional.ofNullable(quarterInspect.getStatus()).isPresent() &&
                quarterInspect.getStatus() != FormStatusConstant.STASH){
            if (!iQuarterInspectService.checkIsExisted(quarterInspect.getId(), quarterInspect.getCode())){
                return new ObjResult(CustomCode.builder().message("单据编码已存在").code(1999).success(false).build());
            }
        }
        boolean result = iQuarterInspectService.submit(quarterInspect);
        return result ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @LogOperation("季度监督检查报告-删除")
    public ObjResult remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return iQuarterInspectService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<QuarterInspectVO> detail(@RequestBody QuarterInspect quarterInspect) {
        QuarterInspect detail = iQuarterInspectService.getOne(Condition.getQueryWrapper(quarterInspect));
        R<QuarterInspectVO> objResult = new R<>(CommonCode.SUCCESS);
        QuarterInspectVO quarterInspectVO = quarterInspectWrapper.entityToVO(detail);
        objResult.setPageData(quarterInspectVO);
        return objResult;
    }
    /**
     * 分页
     */
    @PostMapping("/list")
    public ListResult<QuarterInspectVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("fill_month");
        }
        QueryWrapper<QuarterInspect> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new QuarterInspect()), entityList
        );
        TokenSearch<QuarterInspect> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        IPage<QuarterInspect> pages = iQuarterInspectService.page(Condition.getPage(query), queryWrapper);
        List<QuarterInspectVO> quarterInspectVOList = quarterInspectWrapper.entityToVO(pages.getRecords());
        ListResult<QuarterInspectVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(quarterInspectVOList);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    /**
     *
     * @param quarterInspect
     * @return
     */
    @PostMapping("/initAdd")
    @Resubmit(delaySeconds = 0)
    public R initAdd(@RequestBody QuarterInspect quarterInspect){
        return iQuarterInspectService.initAdd(quarterInspect);

    }

    /**
     * 问题整改报表
     */
    @PostMapping("/orgBydate")
    @Resubmit(delaySeconds = 0)
    public ListResult<PageData> orgBydate(@RequestBody PageData pageData) {
        List<PageData> pageDataList = iQuarterInspectService.orgBydate(pageData);
        ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(pageDataList);
        listResult.setTotal(pageDataList.size());
        return listResult;
    }

    /**
     * 季度报告汇总excel导出
     * @param
     * @return
     */
    @PostMapping(value = "/exportExcel", produces = "application/json;charset=UTF-8")
    public void exportExcel(@RequestBody List<PageData> pdlist, HttpServletResponse response, OutputStream os){
        iQuarterInspectService.exportExcel(pdlist,response,os);
    }

}
